package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.BreakfastBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.BreakfastMapper;
import com.java2207.group1.pojo.Breakfast;
import com.java2207.group1.service.BreakfastService;
import com.java2207.group1.vo.Breakfast.BarVo;
import com.java2207.group1.vo.Breakfast.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author AL 2022/7/26  10:38
 */
@Service("breakfastService")
public class BreakfastServiceImpl implements BreakfastService {

    @Autowired
    private BreakfastMapper breakfastMapper;

    @Override
    public int addBreakfast(Breakfast breakfast) {
        return breakfastMapper.addBreakfast(breakfast);
    }

    @Override
    public int deleteBreakfast(Integer id) {
        return breakfastMapper.deleteBreakfast(id);
    }

    @Override
    public int updateBreakfast(Breakfast breakfast) {
        int i = breakfastMapper.updateBreakfast(breakfast);
        return i;
    }

    @Override
    public LayuiPageVo<Breakfast> breakfastPageDate(Map<String, Object> params) {

        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        //  分页的数据
        PageHelper.startPage(page,limit);
        List<Breakfast> breakfasts = breakfastMapper.selectByExample(params);

        //  mybatis 的分页数据结构
        PageInfo<Breakfast> breakfastPageInfo = new PageInfo<>(breakfasts);

        // 把 mybatis 的分页数据封装到 layui 中
        LayuiPageVo<Breakfast> vo = new LayuiPageVo<>();
        vo.setCode(0);
        vo.setMsg("分页列表数据");
        vo.setCount(breakfastPageInfo.getTotal());
        vo.setData(breakfastPageInfo.getList());
        return vo;
    }

    @Override
    public List<BarVo> nameBarData() {
        List<BarVo> list = breakfastMapper.nameBarData();
        return list;
    }

    @Override
    public List<PieVo> namePieData() {
        List<PieVo> list = breakfastMapper.namePieData();
        return list;
    }

    @Override
    public List<BreakfastBo> breakfastExcelData(Map<String, Object> params) {
        List<Breakfast> breakfasts = breakfastMapper.selectByExample(params);

        List<BreakfastBo> breakfastBos =breakfasts.stream().map((e) -> {
           BreakfastBo breakfastBo = new BreakfastBo();
           breakfastBo.setId(e.getId());
           breakfastBo.setName(e.getName());
           breakfastBo.setTime(e.getTime());
           breakfastBo.setPic(e.getPic());
           breakfastBo.setSpecial(e.getSpecial());
           return breakfastBo;
        }).collect(Collectors.toList());
        return breakfastBos;
    }


}
